<?php
/**
 * Created by PhpStorm.
 * User: mabao
 * 消息：系统消息和聊天消息和聊天
 */
namespace app\api\controller;
use think\Db;
use think\Config;
class Show extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
//    public function _initialize()
//    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            echo json_encode(['status'=>301,'msg'=>"Token失效！请重新登录！"]);
//            exit;
//        }
//    }
    /**
     * 我要装修:getCompany
     * 装修公司、设计案例、设计师（含搜索）、搜索小区
     */
    public function getCompany()
    {
        extract(input());
        if(!isset($type) || !$type){
            $type = 1;
        }
        if($type == 1){
            $data = Db::table('up_company')
                ->order('tuijian DESC,time DESC')
                ->field('name,title,address,thumb,id')
                ->page($page,10)
                ->select();
        }elseif ($type == 2){
            $data = Db::table('up_company_case')
                ->order('cc_time DESC')
                ->field('cc_id,cc_title,cc_name,cc_thumb')
                ->page($page,10)
                ->select();
        }elseif ($type == 3){
            $where = [
                'user_type' => 3 ,
                'user_status' => 1
            ];
            if(isset($keyword) && $keyword){
                $where['user_username'] = ['like','%'.$keyword.'%'];
            }
            $data = Db::table('up_u_user')
                ->where($where)
                ->order('user_reject_time DESC')
                ->field('user_id,user_username,user_shop,user_image,user_phone,user_year_3,user_cases_3')
                ->page($page,10)
                ->select();
        }else{
            $where = [];
            if(isset($keyword) && $keyword){
                $where['dis_name'] = ['like','%'.$keyword.'%'];
            }
            $data = Db::table('up_district')
                ->where($where)
                ->order('dis_update DESC')
                ->page($page,10)
                ->select();
        }

        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 装修公司详情
     */
    public function getCompanyInfo()
    {
        extract(input());
        $data = Db::table('up_company')
            ->where('id',$id)
            ->find();
        if($data['image']){
            $data['image'] = explode(',',$data['image']);
        }
        $designs = Db::table('up_u_user')
            ->where('user_type',3)
            ->where('user_status',1)
            ->where('user_company_id_3',$data['id'])
            ->order('user_reject_time DESC')
            ->field('user_id,user_username,user_shop,user_image,user_phone,user_year_3,user_cases_3')
            ->page(1,10)
            ->select();
        return json(['status'=>200,'msg'=>$data,'designs'=>$designs]);
    }
    /**
     * 案例详情
     */
    public function getCaseInfo()
    {
        extract(input());
        $data = Db::table('up_company_case')->alias('cc')
            ->join ('up_u_user u','cc.cc_u_id = u.user_id','LEFT')
            ->where('cc_id',$cc_id)
            ->field('cc.*,user_id,user_username,user_shop,user_image')
            ->find();
        if($data['cc_image']){
            $data['cc_image'] = explode(',',$data['cc_image']);
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 设计师详情
     */
    public function getDesignInfo()
    {
        header("Access-Control-Allow-Origin:*");
        extract(input());
        $field = 'user_id,user_username,user_shop,user_image,user_phone,user_year_3,user_cases_3,user_signs_3,user_content_3';
        $field .= ',user_chengjiao,user_fuwu,user_fen';
        $data = Db::table('up_u_user')
            ->where('user_id',$uid)
            ->field($field)
            ->find();
        if($data['user_signs_3']){
            $data['user_signs_3'] = explode(',',$data['user_signs_3']);
        }else{
            $data['user_signs_3'] = [];
//            $data['user_signs_3'] = [$data['user_year_3'] . '年经验'];
        }
        $have = Db::table('up_like_design')
            ->where('like_user_id',$user_id)
            ->where('like_user_id_3',$uid)
            ->find ();
        $like = 0;
        if($have){
            $like = 1;//已关注
        }
        //
        if($type == 1){
            $cases = Db::table('up_company_case')
                ->where('cc_u_id',$uid)
                ->order('cc_time DESC')
                ->field('cc_id,cc_title,cc_name,cc_thumb')
                ->page(1,10)
                ->select();
            return json(['status'=>200,'msg'=>$data,'like'=>$like,'cases'=>$cases]);
        }else{
            //---1、最近买卖成交
            $maihouse = Db::table('up_house h')
                ->where('h_user_id',$uid)
                ->where('h_type','>',1)
                ->where('h_done',2)
                ->order('h_done_time DESC')
                ->field(['h_id','h_xiaoqu','h_done_time','h_price','h_unit_price','h_shis','h_tings','h_mianji','h_floor'])
                ->page(1,3)
                ->select();
            //---2、最近租赁成交
            $zuhouse = Db::table('up_house h')
                ->where('h_user_id',$uid)
                ->where('h_type',1)
                ->where('h_done',2)
                ->order('h_done_time DESC')
                ->field(['h_id','h_xiaoqu','h_done_time','h_price','h_unit_price','h_shis','h_tings','h_mianji','h_floor'])
                ->page(1,3)
                ->select();
            //---3、客户评价
            $usercomment = Db::table('up_user_comment uc')
                ->join ('up_u_user u','uc.uc_who_id = u.user_id','LEFT')
                ->where('uc_user_id',$uid)
                ->order('uc_time DESC')
                ->field('uc.*,user_username')
                ->page(1,3)
                ->select();
            if($usercomment){
                foreach ($usercomment as $k => $v){
                    if($v['uc_signs']){
                        $usercomment[$k]['uc_signs'] = explode(',',$v['uc_signs']);
                    }else{
                        $usercomment[$k]['uc_signs'] = [];
                    }
                }
            }
            return json(['status'=>200,'msg'=>$data,'like'=>$like
                ,'maihouse'=>$maihouse
                ,'zuhouse'=>$zuhouse
                ,'usercomment'=>$usercomment
            ]);
        }

    }
    /**
     * 全部评价
     */
    public function allComments()
    {
        extract(input());
        $usercomment = Db::table('up_user_comment uc')
            ->join ('up_u_user u','uc.uc_who_id = u.user_id','LEFT')
            ->where('uc_user_id',$uid)
            ->order('uc_time DESC')
            ->field('uc.*,user_username')
            ->page($page,15)
            ->select();
        if($usercomment){
            foreach ($usercomment as $k => $v){
                if($v['uc_signs']){
                    $usercomment[$k]['uc_signs'] = explode(',',$v['uc_signs']);
                }else{
                    $usercomment[$k]['uc_signs'] = [];
                }
            }
        }
        return json(['status'=>200,'msg'=>$usercomment]);
    }
    /**
     * 获取评价标签
     */
    public function getCommentSigns()
    {
        extract(input());
        $res = Db::table('up_user_comment_sign')->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    /**
     * 发布评价
     */
    public function newComment()
    {
        extract(input());
        $sids = trim($us_ids,',');
        $sign_array = Db::table('up_user_comment_sign')->where('us_id','in',$sids)->column('us_name');
        $data = [
            'uc_user_id' => $uid,
            'uc_who_id' => $user_id,
            'uc_star' => $uc_star,
            'uc_time' => date('Y-m-d H:i:s'),
            'uc_signs' => implode($sign_array,',')
        ];
        $res = Db::table('up_user_comment')->insert($data);
        if($res){
            $all_num = Db::table('up_user_comment')->where('uc_user_id',$uid)->count();
            $all_star = Db::table('up_user_comment')->where('uc_user_id',$uid)->sum('uc_star');
            $fen = round($all_star/$all_num,1);
            Db::table('up_u_user')->where('user_id',$uid)->setField('user_fen',$fen);
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>100,'msg'=>'发布失败，请刷新重试']);
        }
    }
    /**
     * 关注或取消关注设计师
     * uid：设计师id
     */
    public function designLike()
    {
        extract(input());
        $have = Db::table('up_like_design')
            ->where('like_user_id',$user_id)
            ->where('like_user_id_3',$uid)
            ->find ();
        if($have){
            $res = Db::table('up_like_design')
                ->where('like_user_id',$user_id)
                ->where('like_user_id_3',$uid)
                ->delete ();
        }else{
            $data = array();
            $data['like_user_id'] = $user_id;
            $data['like_user_id_3'] = $uid;
            $res = Db::table('up_like_design')->insert($data);
        }
        if($res){
            return json(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败']);
        }

    }
    /**
     * 推荐经纪人（含搜索）
     */
    public function getMiddleman()
    {
        extract(input());
        $where = [
            'user_type' => 2 ,
            'user_status' => 1
        ];
        if(isset($keyword) && $keyword){
            $where['user_username'] = ['like','%'.$keyword.'%'];
        }
        if(isset($shi) && $shi){
            $where['user_shi'] = $shi;
        }
        $data = Db::table('up_u_user')
            ->where($where)
            ->where('user_del', 1)
            ->order('user_time DESC')
            ->field('user_id,user_username,user_shop,user_image,user_phone')
            ->page($page,10)
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 获取带宽利率
     */
    public function getDkLl()
    {
        extract(input());
        if(!isset($type) || !$type){
            $type = 1;
        }
        $data = Db::table('up_dk_ll')
            ->where('dl_type',$type)
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 计算器
     */
    public function jisuanqi()
    {
        extract(input());
        if($type == 1){//1是等额本息，2是等额本金
            //贷款月数，20年就是240个月:(240)
            //贷款总额:(1000000)
            //贷款年利率:(0.049)
            $res = $this->debx($dkm,$dkTotal,$dknl);
        }else{
            $res = $this->debj($dkm,$dkTotal,$dknl);
        }
        $data = $res['data'];
        $alllixi = $res['alllixi'];
        return json(['status'=>200,'msg'=>$data,'alllixi'=>$alllixi]);
    }

    public function debx($dkm,$dkTotal,$dknl)
    {
        $data = array();
        $emTotal = round($dkTotal * $dknl / 12 * pow(1 + $dknl / 12, $dkm) / (pow(1 + $dknl / 12, $dkm) - 1),2); //每月还款金额
        $lxTotal = 0; //总利息
        for ($i = 0; $i < $dkm; $i++) {
            $lx      = round($dkTotal * $dknl / 12,2);   //每月还款利息
            $em      = round($emTotal - $lx,2);  //每月还款本金
            $dkTotal = $dkTotal - $em > 0 ? $dkTotal - $em : 0;
            $data[$i]['num'] = $i + 1;
            $data[$i]['benjin'] = $em;
            $data[$i]['lixi'] = $lx;
            $data[$i]['month'] = $emTotal;
            $data[$i]['last'] = $dkTotal;
            //这是显示第几年--start
//            $data[$i]['year'] = '';
//            if($i % 12 == 0){
//                $data[$i]['year'] = '第' . ($i/12)+1 . '年';
//            }
            //这是显示第几年--end
            $lxTotal = $lxTotal + $lx;
        }
        $res['alllixi'] = $lxTotal;
        $res['data'] = $data;
        return $res;
    }
    protected function debj($dkm,$dkTotal,$dknl)
    {
        $data = array();
        $em      = round($dkTotal / $dkm,2); //每个月还款本金
        $lxTotal = 0; //总利息
        for ($i = 0; $i < $dkm; $i++) {
            $lx      = round($dkTotal * $dknl / 12,2); //每月还款利息
            $dkTotal = $dkTotal - $em > 0 ? $dkTotal - $em : 0;
            $data[$i]['num'] = $i + 1;
            $data[$i]['benjin'] = $em;
            $data[$i]['lixi'] = $lx;
            $data[$i]['month'] = $em + $lx;
            $data[$i]['last'] = $dkTotal;
            //这是显示第几年--start
//            $data[$i]['year'] = '';
//            if($i % 12 == 0){
//                $data[$i]['year'] = '第' . ($i/12)+1 . '年';
//            }
            //这是显示第几年--end
            $lxTotal = $lxTotal + $lx;
        }
        $res['alllixi'] = $lxTotal;
        $res['data'] = $data;
        return $res;

    }


}